package com.h3c.nem.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eos.common.util.DateUtils;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.mapper.TbOfferWorkerMapper;
import com.h3c.nem.utils.DateUtil;
import com.h3c.nem.utils.SendMessageUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@JobHandler(value = "offerWorkerJoinDateSendEmailTask")
public class OfferWorkerJoinDateSendEmailTask extends IJobHandler {

    @Resource
    private SendMessageUtil sendMessageUtil;
    @Resource
    private TbOfferWorkerMapper offerWorkerMapper;

    @Override
    public ReturnT<String> execute(String param) throws Exception {
        QueryWrapper<TbOfferWorker> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbOfferWorker::getPlanReportDate, DateUtil.getDateBegin(new Date()));
        List<TbOfferWorker> workers = offerWorkerMapper.selectList(queryWrapper);

        XxlJobLogger.log("当日入职人数：{}", workers.size());

        String templateCode1 = "WORKER_REPORT_DATE_TEMPLATE";
        String templateCode2 = "REPORT_DATE_SEND_TO_SSC";

        for (TbOfferWorker worker : workers) {
            try {
                Date planReportDate = worker.getPlanReportDate();
                String workCity = worker.getWorkCity();
                String nameChain = worker.getDeptNameChain();
                String workerName = worker.getWorkerName();
                String phoneNo = worker.getPhoneNo();
                String workerBp = sendMessageUtil.getOfferWorkerBp(nameChain);
                String reportDate = DateUtils.dateFormat(planReportDate, DateUtils.DATE_PATTERN);

                //新华三温馨提示：您好，请于今天今天{入职办理时间}前往{入职办理地点}办理入职，请您提前规划路线，携带入职材料，按时到达并联系入职办理人{入职接口人姓名、联系方式}。新华三竭诚期待您的加入。
                Map<String, String> map = new HashMap<>();
                map.put("{入职办理时间}", reportDate);
                map.put("{入职办理地点}", workCity);
                map.put("{入职接口人姓名、联系方式}", workerBp);
                sendMessageUtil.sendEmailToOfferWorker(worker, templateCode1, map, true);

                //您好，您负责部门的预备员工:{部门} {姓名} {联系方式}，预计于今日{入职办理时间}入职报到，请您提前准备好材料，按时接待，感谢。
                Map<String, String> map2 = new HashMap<>();
                map2.put("{部门}", nameChain);
                map2.put("{姓名}", workerName);
                map2.put("{联系方式}", phoneNo);
                map2.put("{入职办理时间}", reportDate);
                sendMessageUtil.sendEmailToSSCAndBp(templateCode2, map2, nameChain, workCity);
            } catch (Exception e) {
                log.error("入职当天发送邮件失败：{}", worker);
                XxlJobLogger.log("入职当天发送邮件失败：{}", worker);
            }
        }

        return ReturnT.SUCCESS;
    }
}
